package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.PdaResultTwOutbTaskDetailBean;
import com.efast.dmom.mgmt.entity.les.TwOutbTaskHeader;
import com.efast.dmom.mgmt.service.les.TwOutbTaskDetailService;
import com.efast.dmom.mgmt.service.les.TwOutbTaskHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 拣货任务表 TW_OUTB_TASK_HEADER。
 * 
 * 扫描或者输入拣货任务单号，显示出这张单据所有信息，页面不可修改任何信息，只是展示，点击确认，进行拣货 7.5.3.1整单拣货
 * 
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwOutbTaskHeader")
@Api(description = "pda->出库->7.5.3.1整单拣货")
public class PdaTwOutbTaskHeaderController {

	private final TwOutbTaskHeaderService twOutbTaskHeaderService;
	private final TwOutbTaskDetailService twOutbTaskDetailService;

	@Autowired
	public PdaTwOutbTaskHeaderController(TwOutbTaskHeaderService twOutbTaskHeaderService,
			TwOutbTaskDetailService twOutbTaskDetailService) {
		this.twOutbTaskHeaderService = twOutbTaskHeaderService;
		this.twOutbTaskDetailService = twOutbTaskDetailService;
	}

	/**
	 * 【确认拣货】
	 * 
	 * @param twOutbTaskHeader
	 *            twOutbTaskHeader
	 */
	@ApiOperation(value = "【pda确认拣货】", notes = "请求参数：so_sheet_no和tw_outb_task_header_id")
	@RequestMapping(value = "/confirmPicking", method = RequestMethod.POST)
	public @ResponseBody ResultBean confirmPicking(@RequestBody TwOutbTaskHeader twOutbTaskHeader) {
		ResultBean result = new ResultBean(true, "'");
		twOutbTaskHeaderService.confirmPicking(twOutbTaskHeader);
		return result;
	}

	/**
	 * pda根据拣货任务单号查询数据
	 * 
	 * @param twOutbTaskHeader
	 *            twOutbTaskHeader
	 */
	@RequestMapping(value = "/query")
	@ApiOperation(value = "【pda根据拣货任务单号查询数据】", notes = "拣货任务单号：pick_alloc_no")
	public @ResponseBody ResultBean cancelPicking(@RequestBody TwOutbTaskHeader twOutbTaskHeader) {
		ResultBean result = new ResultBean(true, "'");
		List<TwOutbTaskHeader> twOutbTaskHeaderList = twOutbTaskHeaderService.selectTwOutbTaskHeader(
				new Assist(Assist.andEq("pick_alloc_no", twOutbTaskHeader.getPickAllocNo())));
		if (twOutbTaskHeaderList.size() == 0) {
			throw new ServiceException("单号不存在");
		}
		List<PdaResultTwOutbTaskDetailBean> twOutbTaskDetailList = twOutbTaskDetailService.queryTaskDetailService(
				twOutbTaskHeaderList.get(0).getTwOutbTaskHeaderId());
		if (twOutbTaskDetailList.size() == 0) {
			throw new ServiceException("单号对应的明细没有数据");
		}
		result.setObject(twOutbTaskDetailList);
		return result;
	}

}
